﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.ExtMethod
{
    public static class Class1
    {
        public static void Invoke(this Control ctrl, MethodInvoker invoker)
        {
            Invoke(ctrl, invoker, null);
        }

        public static void Invoke(this Control ctrl, MethodInvoker invoker, params object[] param)
        {
            if (ctrl != null)
            {
                if (param==null)
                {
                    ctrl.Invoke(invoker);
                }
                else
                {
                    ctrl.Invoke(invoker, param);
                }
            }
        }

        /// <summary>
        ///     在当前控件的线程上,引发指定事件
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="eventHandler"></param>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void RaiseEvent(this Control ctrl, EventHandler eventHandler, object sender, EventArgs e)
        {
            if (ctrl!=null)
            {
                ctrl.Invoke(() => {
                    eventHandler(sender, e);
                });
            }
        }

        /// <summary>
        ///     在当前控件的线程上,引发指定事件
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="eventHandler"></param>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void RaiseEvent<T>(this Control ctrl, EventHandler<T> eventHandler, object sender, T e) where T:EventArgs
        {
            if (ctrl != null)
            {
                ctrl.Invoke(() =>
                {
                    eventHandler(sender, e);
                });
            }
        }

        //public static void Invoke(this Control ctrl, Delegate invoker)
        //{
        //    Invoke(ctrl, invoker, null);
        //}

        //public static void Invoke(this Control ctrl, Delegate invoker, params object[] param)
        //{
        //    Invoke(ctrl, () => invoker.Method.FastInvoke(invoker.Target, param));
        //}
    }
}
